package com.google.code.mochaccino.framework.ui.freemarker;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.ui.components.Option;
import com.google.common.base.Preconditions;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.CollectionModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.commons.lang3.StringUtils;

/**
 *
 */
public class TemplateUtil {

	/** Return the Locale List Model */
	public static final CollectionModel getLocaleListModel( List<Locale> locales, String selected ) {
		return new CollectionModel( getLocaleList( locales, selected ), BeansWrapper.getDefaultInstance() );
	}

	/** Return an option list for specified locales */
	public static List<Option> getLocaleList( List<Locale> locales, String selected ) {
		List<Option> optionList = new ArrayList<Option>();
		for ( Locale loc : locales ) {
			Option opt = new Option( loc.toString(), StringUtils.capitalize( loc.getDisplayLanguage( loc ) ), false );
			optionList.add( opt );
			if ( selected != null && opt.getValue() != null && opt.getValue().equals( selected ) ) {
				opt.setSelected( true );
			}
		}
		return optionList;
	}

	/** Create the Option List */
	public static final List<Option> createOptionList( Enum[] values, ResourceBundle bundle ) {
		Preconditions.checkNotNull( values, "Enum values cannot be null" );
		Preconditions.checkNotNull( bundle, "Bundle cannot be null" );
		List<Option> optList = new ArrayList<Option>();
		for ( Enum e : values ) {
			String value = bundle.getString( e.name() );
			if ( value == null ) {
				value = e.name();
			}

			Option opt = new Option( e.name(), value );
			optList.add( opt );
		}
		return optList;
	}

	/** Return a Freemarker List Model Collection for this enumeration */
	public static final CollectionModel createOptionListModel( Class<Enum> enumClass, ResourceBundle bundle ) {
		return new CollectionModel( createOptionList( enumClass, bundle ), BeansWrapper.getDefaultInstance() );
	}

	/** Create the Option List */
	public static final List<Option> createOptionList( Class<Enum> enumClass, ResourceBundle bundle ) {
		Preconditions.checkNotNull( enumClass, "Enum cannot be null" );
		return createOptionList( enumClass.getEnumConstants(), bundle );
	}

	/** Return a Freemarker List Model Collection for this enumeration */
	public static final CollectionModel createOptionListModel( Enum[] values, ResourceBundle bundle ) {
		return new CollectionModel( createOptionList( values, bundle ), BeansWrapper.getDefaultInstance() );
	}
}
